<?php
class model_intro extends database_mysql
{
    public function mdi_intro_list($cid = null,$page=1,$limit=50)
    {
        $str = null;
        if(!empty($limit))
        {
			$page=($page==0 || empty($page)?1:$page);
			$start = ($page - 1)* $limit; 
			$strlimit = "LIMIT $start, $limit";
		}
		else{
			$strlimit ='';
		}		 
        
        if($cid){
            $cid = parent::mysql_check_string($cid);
            $str = " ". prefix ."categories.ID = '".$cid."' AND ";
        }
        
        $row = parent::mysql_fetch_all_rows("
                SELECT
                ". prefix ."categories_lang.Title,
                ". prefix ."categories_lang.SeoKey,
                ". prefix ."categories_lang.`Status`,
                ". prefix ."categories.ID
                FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
                WHERE
                ".$str."
                ". prefix ."categories.Com = 'intro' AND
                ". prefix ."categories.Define = 'sub' AND
                ". prefix ."categories_lang.LangID = ".LANG."
                ORDER BY ". prefix ."categories.ID DESC ".$strlimit."
        ");
        parent::mysql_close_connection();
 		return $row;
    }
    
	public function mdi_intro_total_list()
    {
        
        $sql = "SELECT COUNT( ID ) AS TOTAL
                FROM
                ". prefix ."categories_lang
                INNER JOIN ". prefix ."categories ON ". prefix ."categories_lang.CatID = ". prefix ."categories.ID
                WHERE
                ". prefix ."categories.Com = 'intro' AND
                ". prefix ."categories.App = 'public' AND
                ". prefix ."categories.Define = 'sub' AND
                ". prefix ."categories_lang.LangID = 
                " .LANG;
        
    }
    
    public function mdi_intro_get_cid($seokey = null)
    {
        $str = null;
        $row = parent::mysql_fetch_one_rows("
                SELECT
                *
                FROM
                ". prefix ."intro INNER JOIN ". prefix ."intro_lang 
                ON ". prefix ."intro.ID = ". prefix ."intro_lang.introID
                WHERE
                ". prefix ."intro_lang.SeoKey = '".$seokey."'
                ". prefix ."intro_lang.LangID = '".LANG."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdi_get_cat_cid($seokey)
    {
        $row = null;
        if(!empty($seokey))
        {
            $seokey = parent::mysql_check_string($seokey);
            $row = parent::mysql_fetch_one_rows("SELECT  * FROM ". prefix ."categories_lang WHERE SeoKey = '".trim($seokey)."' AND LangID = '".LANG."'");
            parent::mysql_close_connection();
        }
 		return $row;
    }
    
    public function mdi_get_art($artid)
    {
        $artid = parent::mysql_check_string($artid);
        
        $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                ". prefix ."intro
            WHERE
                ". prefix ."intro.ID = '".trim($artid)."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdi_get_art_lang($artid,$lang_id)
    {
        $artid = parent::mysql_check_string($artid);
        
        $row =   parent::mysql_fetch_one_rows("
            SELECT * FROM
                ". prefix ."intro_lang
            WHERE
                ". prefix ."intro_lang.introID = '".trim($artid)."' AND
                ". prefix ."intro_lang.LangID = '".$lang_id."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdi_get_seo_key($seokey,$lang_id,$artid=null)
    {
        $str = null;
        $artid = parent::mysql_check_string($artid);
        $seokey = parent::mysql_check_string($seokey);
        if(!empty($artid))
        {
            $str = prefix ."intro_lang.introID <> '".trim($artid)."' AND ";
        }
        $row = parent::mysql_fetch_one_rows("
            SELECT count(introID) as total FROM
                ". prefix ."intro_lang
            WHERE
                ".$str."
                ". prefix ."intro_lang.SeoKey = '".trim($seokey)."' AND
                ". prefix ."intro_lang.LangID = '".$lang_id."'
        ");
        
        parent::mysql_close_connection();
        
 		return isset($row['total'])?$row['total']:NULL;
    }
    
    public function mdi_art_insert()
    {
        $data = array(
            'CID' => isset($_POST['CID'])&& !empty($_POST['CID'])?$_POST['CID']:0,
            'Order' => isset($_POST['Order'])&& !empty($_POST['Order'])?$_POST['Order']:0,
            'introHot' => isset($_POST['introHot'])&& !empty($_POST['introHot'])?1:0,
            'NoIndexFollow' => isset($_POST['NoIndexFollow'])&& !empty($_POST['NoIndexFollow'])?1:0,
            'DatePost' => isset($_POST['DatePost'])&& !empty($_POST['DatePost'])? strtotime($_POST['DatePost']):0,
            'ActiveDatePost' => isset($_POST['ActiveDatePost'])&& !empty($_POST['ActiveDatePost'])?1:0,
            'PostDate'=>(string)time(),
            'UserID'=>$_SESSION['ID'],
            'Picture'=>isset($_POST['Img'])?$_POST['Img']:null
        );

        $rs = parent::mysql_insert_array(prefix."intro",$data);
        $id = mysql_insert_id();
        parent::mysql_close_connection();
        return $id;                
    }
    
    public function mdi_art_insert_lang($id,$lang)
    {
        $data = array(
            'introID'=>$id,
            'LangID'=>$lang,
            'AltPic' => $_POST['AltPic'][$lang],
            'Name' => $_POST['Name'][$lang],
            'FullContents' => $_POST['FullContents'][$lang],
            'Locks' => isset($_POST['Locks'][$lang])?$_POST['Locks'][$lang]:0,
            'SeoKey' => $_POST['SeoKey'][$lang],
            'Title' => $_POST['Title'][$lang],
            'Desc' => $_POST['Desc'][$lang],
            'Keyword' => $_POST['Keyword'][$lang],
            'TitleH1' => $_POST['TitleH1'][$lang],
            'TitleH2' => $_POST['TitleH2'][$lang],
            'DescF' => $_POST['DescF'][$lang],
            'Status' => $_POST['Status'][$lang]
        );
        $rs = parent::mysql_insert_array(prefix."intro_lang",$data);
        parent::mysql_close_connection();
        return $rs;  
                
    }
    
    public function mdi_art_update($id)
    {
        $data = array(
            'CID' => isset($_POST['CID'])&& !empty($_POST['CID'])?$_POST['CID']:0,
            'Order' => isset($_POST['Order'])&& !empty($_POST['Order'])?$_POST['Order']:0,
            'introHot' => isset($_POST['introHot'])&& !empty($_POST['introHot'])?1:0,
            'NoIndexFollow' => isset($_POST['NoIndexFollow'])&& !empty($_POST['NoIndexFollow'])?1:0,
            'DatePost' => isset($_POST['DatePost'])&& !empty($_POST['DatePost'])? strtotime($_POST['DatePost']):0,
            'ActiveDatePost' => isset($_POST['ActiveDatePost'])&& !empty($_POST['ActiveDatePost'])?1:0,
            'EditDate'=>(string)time(),
            'UserID'=>$_SESSION['ID']
        );
        if(isset($_POST['Img']) && !empty($_POST['Img']))
        {
            $data['Picture'] = $_POST['Img'];
        }
        
        $rs = parent::mysql_update_array(prefix."intro",$data," ID='".$id."' ");
        parent::mysql_close_connection();
        return $rs; 
        
    }
    public function mdi_art_update_lang($id,$lang)
    {
        $rs=null;              
        
        $aid = $this->mda_get_art_lang($id,$lang);
        $xid = count($aid);
        
        if($xid>0)
        {
            $data = array(
                'AltPic' => $_POST['AltPic'][$lang],
                'Name' => $_POST['Name'][$lang],
                'FullContents' => $_POST['FullContents'][$lang],
                'Locks' => isset($_POST['Locks'][$lang]) && !empty($_POST['Locks'][$lang])?$_POST['Locks'][$lang]:0,
                'SeoKey' => $_POST['SeoKey'][$lang],
                'Title' => $_POST['Title'][$lang],
                'Desc' => $_POST['Desc'][$lang],
                'Keyword' => $_POST['Keyword'][$lang],
                'TitleH1' => $_POST['TitleH1'][$lang],
                'TitleH2' => $_POST['TitleH2'][$lang],
                'DescF' => $_POST['DescF'][$lang],
                'Status' => $_POST['Status'][$lang]
            );
            $rs = parent::mysql_update_array(prefix."intro_lang",$data,"introID='".$id."' AND LangID='".$lang."'");
            
        }else
        {
            $seo_key = $this->mda_get_seo_key($_POST['SeoKey'][$lang],$lang);
            if($seo_key<1)
            {
                $data = array(
                    'introID'=>$id,
                    'LangID'=>$lang,
                    'AltPic' => $_POST['AltPic'][$lang],
                    'Name' => $_POST['Name'][$lang],
                    'FullContents' => $_POST['FullContents'][$lang],
                    'Locks' => isset($_POST['Locks'][$lang]) && !empty($_POST['Locks'][$lang])?$_POST['Locks'][$lang]:0,
                    'SeoKey' => $_POST['SeoKey'][$lang],
                    'Title' => $_POST['Title'][$lang],
                    'Desc' => $_POST['Desc'][$lang],
                    'Keyword' => $_POST['Keyword'][$lang],
                    'TitleH1' => $_POST['TitleH1'][$lang],
                    'TitleH2' => $_POST['TitleH2'][$lang],
                    'DescF' => $_POST['DescF'][$lang],
                    'Status' => $_POST['Status'][$lang]
                );
                $rs = parent::mysql_insert_array(prefix."intro_lang",$data);
            }
        }
            
        parent::mysql_close_connection();
        return $rs;  
        
    }

    public function mdi_art_del_pic($id)
    {
        $id = parent::mysql_check_string($id);
        $update = "UPDATE ".prefix."intro SET Picture='' WHERE `ID` = '".$id."'";
        $done = parent::mysql_update_insert($update);
        parent::mysql_close_connection();
        return $done;
    }

    public function mdi_art_get_pic($id)
    {
        $id = parent::mysql_check_string($id);
        $sql = "SELECT Img FROM ".prefix."categories WHERE `ID` = '".$id."'";
        $row = parent::mysql_fetch_one_rows($sql);
        parent::mysql_close_connection();
        return isset($row['Picture'])?$row['Picture']:null;
    }

    public function mdi_tool_func($model,$active,$str_id)
    {
        $done = false;

        switch($active)
        {
            case 'show':
            case 'hide':
            case 'del':
                $done = $model->model_tool_set_status(prefix."categories_lang",$active," CatID IN(".trim($str_id).")");
            break;

            case 'delete':
                $arr = explode(',',$str_id);
                if(!empty($arr))
                {
                    foreach($arr as $id)
                    {
                        $img = $this->mda_art_get_pic($id);
                        if(!empty($img))
                        unlink(PATH_BASE.'upload/images/categories/'.$img);
                    }
                }
                $done = $model->model_tool_delete(prefix."intro"," ID IN(".trim($str_id).")" );
                $done = $model->model_tool_delete(prefix."intro_lang"," introID IN(".trim($str_id).")");
                $done = $model->model_tool_delete(prefix."categories_lang"," CatID IN(".trim($str_id).")");
                $done = $model->model_tool_delete(prefix."categories"," ID IN(".trim($str_id).")");

            break;
        }

        return $done;
    }
    
    public function mdi_intro_detail($alias, $lang_id){
        $row = parent::mysql_fetch_one_rows("
                SELECT
                ".prefix."categories_lang.SeoKey,
                ".prefix."categories_lang.CatID,
                ".prefix."intro_lang.FullContents,
                ".prefix."categories_lang.CatName
                FROM
                ".prefix."categories
                INNER JOIN ".prefix."categories_lang ON ".prefix."categories_lang.CatID = ".prefix."categories.ID
                LEFT JOIN ".prefix."intro ON ".prefix."intro.ID = ".prefix."categories.ID
                LEFT JOIN ".prefix."intro_lang ON ".prefix."intro_lang.IntroID = ".prefix."intro.ID
                WHERE
                ".prefix."categories_lang.SeoKey = '$alias' AND
                ".prefix."intro_lang.LangID = $lang_id
        ");
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdi_intro_detail_edit($id, $lang_id){
        $row = parent::mysql_fetch_one_rows("
                SELECT
                ".prefix."intro_lang.FullContents
                FROM
                ".prefix."intro
                LEFT JOIN ".prefix."intro_lang ON ".prefix."intro.ID = ".prefix."intro_lang.IntroID
                WHERE
                ".prefix."intro_lang.LangID = $lang_id AND
                ".prefix."intro.ID = $id
        ");
        
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdi_intro_exit($id){
        $row = parent::mysql_fetch_one_rows("
                SELECT
                ".prefix."intro.ID
                FROM
                ".prefix."intro
                WHERE
                ".prefix."intro.ID = $id
        ");
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdi_intro_lang_exit($id, $lang_id){
        $row = parent::mysql_fetch_one_rows("
                SELECT
                ".prefix."intro_lang.FullContents
                FROM
                ".prefix."intro
                LEFT JOIN ".prefix."intro_lang ON ".prefix."intro.ID = ".prefix."intro_lang.IntroID
                WHERE
                ".prefix."intro.ID = $id AND
                ".prefix."intro_lang.LangID = $lang_id


        ");
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdi_intro_status($alias){
        $row = parent::mysql_fetch_one_rows("
                SELECT
                ".prefix."categories_lang.SeoKey,
                ".prefix."categories_lang.CatID,
                ".prefix."intro_lang.FullContents
                FROM
                ".prefix."categories
                INNER JOIN ".prefix."categories_lang ON ".prefix."categories_lang.CatID = ".prefix."categories.ID
                INNER JOIN ".prefix."intro ON ".prefix."intro.ID = ".prefix."categories.ID
                INNER JOIN ".prefix."intro_lang ON ".prefix."intro_lang.IntroID = ".prefix."intro.ID
                WHERE
                ".prefix."categories_lang.SeoKey = '$alias' AND
                ".prefix."intro_lang.LangID = $lang_id
        ");
        parent::mysql_close_connection();
 		return $row;
    }
    
    public function mdi_intro_insert($table, $data){
        parent::mysql_insert_array(prefix.$table, $data);
    }
    public function mdi_intro_update($table,$data, $condition){
        parent::mysql_update_array(prefix.$table, $data, $condition);
    }
    
    public function cmdi_check_seokey($seoKey){
        $sql = "
                SELECT
                ".prefix."categories_lang.SeoKey
                FROM
                ".prefix."categories_lang
                WHERE
                ".prefix."categories_lang.SeoKey = '$seoKey'
        ";
        return parent::mysql_fetch_one_rows($sql);
    }

    
}